package com.titan.core.utils.data;

/**
 * @author 周成功
 * @email 86898837@qq.com
 * @date 2020年02月10日
 */
public class AssertUtils {
    public static <T> T fail(String message) {
        throw new IllegalArgumentException(message);
    }
    public static <T> T fail(Class clazz, String method, String message) {
        String msg = message(clazz, method, message);
        return AssertUtils.fail(msg);
    }    

    
    private static String messageEquals(String prefix, String expected, String actual) {
        StringBuilder buf = new StringBuilder(128);
        buf.append(prefix==null?"":prefix);
        buf.append("实际值[").append(actual).append("]，与期望的值[").append(expected).append("]不同！");
        return buf.toString();
    }
    public static String message(Class clazz, String methodName, String msg) {
        return message(clazz.getName(), methodName, msg);
    }
    private static String message(String className, String methodName, String msg) {
        StringBuilder buf = new StringBuilder(128);
        buf.append("在执行类").append(className);
        buf.append("的方法").append(methodName);
        buf.append("时发生错误，错误信息为：").append(msg);
        return buf.toString();
    }    
    
    public static void assertTrue(boolean expression, String message) {
        if (!expression) {
            fail(message);
        }
    }
    public static void assertTrue(boolean expression, Class clazz, String method, String message) {
        String msg = message(clazz, method, message);
        AssertUtils.assertTrue(expression, msg);
    }    
    public static void assertFalse(boolean expression, String message) {
        if (expression) {
            fail(message);
        }
    }    
    public static void assertFalse(boolean expression, Class clazz, String method, String message) {
        String msg = message(clazz, method, message);
        AssertUtils.assertFalse(expression, msg);
    }
    
    public static void assertNull(Object object, String message) {
        assertTrue((object==null), message);
    }
    public static void assertNotNull(Object object, String message) {
        assertTrue((object!=null), message);
    }
    public static void assertNotNull(Object arg, Class clazz, String method, String argName) {
        String message = message(clazz, method, "方法参数["+argName+"]不能为空！");
        AssertUtils.assertNotNull(arg, message);
    }
    
    
    public static boolean isContains(Object value, Object...items) {
        if(items==null || items.length==0) {
            return false;
        }
        for (int i = 0; i < items.length; i++) {
            if (value == items[i]) {
                return true;
            }
            if (value == null || items[i] == null) {
                continue;
            }
            if(value.equals(items[i])) {
                return true;
            }
        }
        return false;
    }
    public static boolean isNotContains(Object value, Object...items) {
        return !isContains(value, items);
    }
    public static boolean isContainsIgnoreCase(String value, String...items) {
        if(items==null || items.length==0) {
            return false;
        }
        for (int i = 0; i < items.length; i++) {
            if (value == items[i]) {
                return true;
            }
            if (value == null || items[i] == null) {
                continue;
            }
            if(value.equalsIgnoreCase(items[i])) {
                return true;
            }
        }
        return false;
    }
    
    public static void assertEmpty(String value, String message) {
        assertTrue(StringUtils.isEmpty(value), message);
    }
    public static void assertNotEmpty(String value, String message) {
        assertTrue(StringUtils.isNotEmpty(value), message);
    }
    public static void assertNotEmpty(String value, Class<?> clazz, String method, String argName) {
        String message = message(clazz, method, "方法参数["+argName+"]不能为空！");
        AssertUtils.assertNotEmpty(value, message);
    }    
    
    public static void assertEquals(String expected, String actual) {
        if( (expected==null)&&(actual==null) ) {
            return;
        }
        String message = messageEquals("", expected, actual);
        assertTrue( ((expected!=null)&&(actual!=null)), message);
        assertTrue( expected.equals(actual), message );
    }
    
    
    
    
}
